package manage.action;

import m.common.action.ActionMeta;
import m.common.action.VueResult;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.html.VueFormResult;
import m.html.VueListResult;
import m.html.element.attr.ButtonType;
import m.html.form.FormButton;
import m.html.form.FormEvent;
import m.html.form.FormInput;
import m.html.form.ModelForm;
import m.html.list.*;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.model.DictionaryData;
import manage.model.DictionaryType;
import manage.service.DictionaryDataService;
import manage.util.DictionaryUtil;

@ActionMeta(name="manage/dictionaryData")
public class DictionaryDataAction extends StatusAction {

	private DictionaryData model;
	public JSONMessage doSave() throws Exception {
		systemLog("字典","保存字典信息");
		verifyAdminOperPower("manage_system_power");
		getService(DictionaryDataService.class).save(model);
		DictionaryType type=ModelQueryList.getModel(DictionaryType.class,model.getDictionaryType().getOid(),"type");
		DictionaryUtil.clear(type.getType());
		return new JSONMessage(0,"保存成功");
	}
	
	@Override
	public void doDisableCallback() {
		DictionaryUtil.clearAll();
	}
	@Override
	public void doRecoveryCallback() {
		DictionaryUtil.clearAll();
	}
	public VueResult toEdit() throws Exception {
		verifyAdminOperPower("manage_system_power");
		if(null!=model&&!StringUtil.isSpace(model.getOid())) {
			model=ModelQueryList.getModel(model,"*");
		}else if(null==model){
			throw new MException(this.getClass(),"参数错误");
		}
		ModelForm form = new ModelForm("字典值",1);
		form.addItem(
			FormInput.input("名称", "model.name").setPlaceholder("请输入名称").setRequired(true),
			FormInput.input("值", "model.value").setPlaceholder("请输入值").setRequired(true).setDisabled("model.oid"),
			FormInput.number("排序", "model.sort").setPlaceholder("请输入排序")
		)
		.addData("model",model)
		.addButton(
			FormButton.pri("保存","doSave")
		)
		.addMethod(
			FormEvent.backTrue,
			FormEvent.doJson("doSave",actionUrl("doSave"),"{model:this.model}")
				.success("backTrue")
		);
		VueFormResult result=new VueFormResult();
		return result.addForm(form);
	}
	public VueResult dictionayDataList() throws Exception{
		verifyMenu("A1105");
		boolean power=hasAdminOperPower(getStatusPower());
		String name=getParamsValue("parentData.name");
		String value=getParamsValue("parentData.value");
		String title=StringUtil.isSpace(name)?"":(name+" ("+value+") ");
		VueListResult result=getVueListResult(title+"字典值",DictionaryData.class,actionUrl("dictionayDataList"),true);
		result.setHeight(-1);
		result.addCol(toTextColumn("名称", "name",150,true).setSort(true));
		result.addCol(toTextColumn("值", "value",true));
		result.addCol(toTextColumn("排序", "sort").setSort(ListSortOrder.ASC));
		result.addCol(toStatusColumn(power,true));
		if(power) result.addCol(toSpaceColumn("操作",120,
			ListButton.drawer("修改", ButtonType.PRIMARY,actionUrl("toEdit"),500, SuccessMethod.REFRESH)
				.addParam("model.oid","oid"),
			ListButton.page("子项",ButtonType.LINK,actionUrl("dictionayDataList"))
					.addParam("params[dictionaryType.oid]","dictionaryType.oid").addParam("params[parentData.oid]","oid")
					.addParam("params[parentData.name]","name").addParam("params[parentData.value]","value")
		));
		if(power) result.addButton(
			ListButton.drawer("添加",ButtonType.PRIMARY,actionUrl("toEdit"),500,SuccessMethod.REFRESH)
				.addQueryParam("model.dictionaryType.oid", "dictionaryType.oid").addQueryParam("model.parentData.oid", "parentData.oid")
		);
		result.addSearch(ListSearch.field("","dictionaryType.oid", SearchType.HIDDEN_EQ));
		result.addSearch(ListSearch.field("","parentData.oid",SearchType.HIDDEN_EQ));
		if(StringUtil.isSpace(getParamsValue("parentData.oid"))){
			result.addCondition(QueryCondition.isEmpty("parentData.oid"));
		}
		return result;
	}
	@Override
	public String[] getStatusPower() {
		return new String[]{"manage_system_power"};
	}
	public DictionaryData getModel() {
		return model;
	}
	public void setModel(DictionaryData model) {
		this.model = model;
	}

}
